package kr.ac.kaist.savehearts.ui;

import kr.ac.kaist.savehearts.Configure;
import kr.ac.kaist.savehearts.auth.FacebookConnection;
import kr.ac.kaist.savehearts.auth.FacebookLoginListener;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class StartActivity extends Activity implements OnClickListener, FacebookLoginListener {

	private static final String TAG = StartActivity.class.getName();
	
	/**
	 * [Login] button on StartActivity (start.xml)
	 */
	private Button buttonLogin;
	
	/**
	 * [Sign up] button on StartActivity (start.xml)
	 */
	private Button buttonSignup;
	
	/**
	 * [Sign in with Facebook] image button on StartActivity (start.xml)
	 */
	private ImageButton buttonFacebookLogin;
	
	/**
	 * FacebookConnection class used for authentication using facebook account
	 */
	private FacebookConnection fbConn;
	
	/**
	 * ProgressDialog to show when the authentication process is running
	 */
	private ProgressDialog dialog;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);
		
		buttonLogin = (Button)findViewById(R.id.buttonStartLogin);
		buttonLogin.setOnClickListener(this);
		
		buttonSignup = (Button)findViewById(R.id.buttonStartSignUp);
		buttonSignup.setOnClickListener(this);
		
		buttonFacebookLogin = (ImageButton)findViewById(R.id.ImageButtonFacebookLogin);
		buttonFacebookLogin.setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ( fbConn != null ) {
			fbConn.getFacebookInstance().authorizeCallback(requestCode, resultCode, data);
			fbConn.updateFacebookConnection();
		}
	}


	@Override
	public void FacebookLoginCompleted(boolean bSuccess) {
		// finished login and received user information.
		if ( bSuccess ){
			String userID = fbConn.getUserInfo().getId();
			String lastName = fbConn.getUserInfo().getLastName();
			String firstName = fbConn.getUserInfo().getFirstName();
			String userEmail = fbConn.getUserInfo().getEmail();
			String userName = fbConn.getUserInfo().getFullName();
			Log.d(TAG, userID + "," + userName + "," + userEmail);
			
			// Set user information received from facebook account
			Configure.getInstance().setUserInfo(fbConn.getUserInfo());
		}
		else
			Log.e(TAG, "Log in failed!");
		
		if ( dialog != null ) dialog.dismiss();
		dialog = null;
	}

	@Override
	public void onClick(View v) {
		Intent myIntent;
		switch (v.getId()) {
			case R.id.buttonStartLogin:
				myIntent = new Intent(StartActivity.this, LoginActivity.class);
				startActivity(myIntent);
				break;
				
			case R.id.buttonStartSignUp:
				myIntent = new Intent(StartActivity.this, SignupActivity.class);
				startActivity(myIntent);
				break;
				
			case R.id.ImageButtonFacebookLogin:
				// Signin in with facebook account
				Log.d(TAG, "Start facebook authentication...");
				dialog = ProgressDialog.show(StartActivity.this, "", getString(R.string.please_wait));
				// get singleton object to authenticate the user through facebook
				fbConn = FacebookConnection.getInstance(this, this);
				// try to log in facebook
				fbConn.loginFacebook();
				break;
		}		
	}
}
